package com.isoft.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.isoft.course.domain.StudentCourse;
import com.isoft.course.service.IStudentCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我要选课Controller
 * 
 * @author lgm
 * @date 2024-07-20
 */
@RestController
@RequestMapping("/studentCourse/studentCourse")
public class StudentCourseController extends BaseController
{
    @Autowired
    private IStudentCourseService studentCourseService;

    /**
     * 查询我要选课列表
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentCourse studentCourse)
    {
        startPage();
        List<StudentCourse> list = studentCourseService.selectStudentCourseList(studentCourse);
        return getDataTable(list);
    }
    /**
     * 查询所有老师
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:list')")
    @GetMapping("/teacherList")
    public AjaxResult teacherList()
    {
        return success(studentCourseService.selectTeacherList());
    }

    /**
     * 导出我要选课列表
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:export')")
    @Log(title = "我要选课", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentCourse studentCourse)
    {
        List<StudentCourse> list = studentCourseService.selectStudentCourseList(studentCourse);
        ExcelUtil<StudentCourse> util = new ExcelUtil<StudentCourse>(StudentCourse.class);
        util.exportExcel(response, list, "我要选课数据");
    }

    /**
     * 获取我要选课详细信息
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentCourseService.selectStudentCourseById(id));
    }

    /**
     * 新增我要选课
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:add')")
    @Log(title = "我要选课", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentCourse studentCourse)
    {
        return toAjax(studentCourseService.insertStudentCourse(studentCourse));
    }

    /**
     * 修改我要选课【选课操作】
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:edit')")
    @Log(title = "我要选课", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentCourse studentCourse)
    {
        if (studentCourseService.updateStudentCourse(studentCourse)>0){
            return success();
        }else if (studentCourseService.updateStudentCourse(studentCourse)==0){
            return error("选课失败，您选择的课已满！");
        }else if (studentCourseService.updateStudentCourse(studentCourse)==-1){
            return error("选课失败，您已选过该课程！");
        }else {
            return error();
        }
    }
    /**
     * 修改我要选课【退课操作】
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:edit')")
    @Log(title = "我要退课", businessType = BusinessType.UPDATE)
    @PutMapping("/exit")
    public AjaxResult deledit(@RequestBody StudentCourse studentCourse)
    {
        if (studentCourseService.deupdateStudentCourse(studentCourse)>0){
            return success();
        }else {
            return error();
        }
    }

    /**
     * 删除我要选课
     */
    @PreAuthorize("@ss.hasPermi('studentCourse:studentCourse:remove')")
    @Log(title = "我要选课", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentCourseService.deleteStudentCourseByIds(ids));
    }
}
